package com.ad.pet.app.account;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.account.Account;
import com.ad.pet.models.remote.account.AccountPet;
import com.ad.pet.models.remote.account.pet.PetCateFirst;
import com.ad.pet.models.remote.account.pet.PetColor;
import com.ad.pet.models.result.ResultBean;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/4/2.
 */
@PerActivity
public class AccountPresenter extends AbsBasePresenter implements AccountContract.Presenter {

    private ApiService mApiService;
    private AccountContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public AccountPresenter(ApiService apiService,
                           Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
        mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void onCreate(AccountContract.View view) {
        this.mView = view;
    }



    @Override
    public void loginOut(String accToken) {
        mRxDisposable.add(mApiService.loginOut(accToken)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.loginOutSuccess();
                    }
                }));
    }

    @Override
    public void getUserInfo() {
        mRxDisposable.add(mApiService.get_user_info()
                .subscribe(new AppSubscriber<Account>(mView) {
                    @Override
                    public void next(Account keyWord) {
                        mView.getUserInfoSuccess(keyWord);
                    }
                }));
    }

    @Override
    public void getPetInfo(String petId) {
        mRxDisposable.add(mApiService.getPetInfo(petId)
                .subscribe(new AppSubscriber<AccountPet>(mView) {
                    @Override
                    public void next(AccountPet keyWord) {
                        mView.getPetInfoSuccess(keyWord);
                    }
                }));
    }


    @Override
    public void uploadHead(File file,String type) {
        mRxDisposable.add(mApiService.upload_avatar(file,type)
                .subscribe(new AppSubscriber<ResultBean<String>>(mView) {
                    @Override
                    public void next(ResultBean<String> keyWord) {
                        mView.uploadHead(keyWord.getResponseData());
                    }
                }));
    }

    @Override
    public void upPetInfo(String pet_headimg, String pet_name, String sex, String birthday, String category_id, String weight, String is_sterilisate, String color_id, String pet_id) {
        mRxDisposable.add(mApiService.upPetInfo( pet_headimg,  pet_name,  sex,  birthday,  category_id,  weight,  is_sterilisate,  color_id,  pet_id)
                .subscribe(new AppSubscriber<List>(mView) {
                    @Override
                    public void next(List user) {
                        mView.upPetInfoSuccesss();
                    }
                }));
    }

    @Override
    public void getPetCategory() {
        mRxDisposable.add(mApiService.getPetCate()
                .subscribe(new AppSubscriber<List<PetCateFirst>>(mView) {
                    @Override
                    public void next(List<PetCateFirst> keyWord) {
                        mView.getPetCategorySuccess(keyWord);
                    }
                }));
    }

    @Override
    public void getPetColor() {
        mRxDisposable.add(mApiService.getPetColor()
                .subscribe(new AppSubscriber<List<PetColor>>(mView) {
                    @Override
                    public void next(List<PetColor> keyWord) {
                        mView.getPetColorSuccess(keyWord);
                    }
                }));
    }

    @Override
    public void deletePet(String petId) {
        mRxDisposable.add(mApiService.deletePet(petId)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User keyWord) {
                        mView.deletePetSuccess();
                    }
                }));
    }

    @Override
    public void upPersonInfo(String user_headimg, String user_name, String nick_name, String sex, String birthday, String user_tel) {
        mRxDisposable.add(mApiService.upUserinfo( user_headimg,  user_name,  nick_name,  sex,  birthday,  user_tel)
                .subscribe(new AppSubscriber<List>(mView) {
                    @Override
                    public void next(List keyWord) {
                        mView.upPersonInfoSuccess();
                    }
                }));
    }

}
